package gold.digger;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC343 {
    public long startExecuteTime = System.currentTimeMillis();

    class Solution {
        public Map<Integer, Integer> memo = new HashMap<Integer, Integer>();

        public int integerBreak(int n) {
            if (n <= 3) return n - 1;
            return integerBreakRecur(n);
        }

        public int integerBreakRecur(int n) {
            if (n <= 3) return n;
            if (memo.containsKey(n)) return memo.get(n);

            int curMax = Integer.MIN_VALUE;
            for (int i = 2; i < n; i++) {
                int left = integerBreakRecur(i);
                int right = integerBreakRecur(n - i);
                int cur = left * right;
                if (curMax < cur) curMax = cur;
            }

            memo.put(n, curMax);
            return curMax;
        }
    }


    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.integerBreak(10));
    }

    public static void main(String[] args) throws Exception {
        LC343 an = new LC343();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
